package com.mpyy.handler.systemSetting;

import com.alibaba.fastjson.JSON;
import com.mpyy.dao.ToolInter;
import com.mpyy.pojo.SupplierInfo;
import com.mpyy.service.systemSetting.SupplierInformationServiceInter;
import com.mpyy.tool.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @Descrioption SupplierInformationController
 * @Author QIAO
 * @Date 2022-03-23 16:01
 */
@Controller
public class SupplierInformationController {
    @Autowired
    @Qualifier(value = "supplierInformationServiceImpl")
    private SupplierInformationServiceInter supplierInformationServiceInter;

    @Autowired
    private ToolInter toolInter;

    /**
     * 查找供应商数据
     * @param request
     * @param supplierInfo
     * @return
     */
    @RequestMapping(value = {"/showSearchSupplierInfoData"})
    @ResponseBody
    public String showSearchSupplierInfoData(HttpServletRequest request , SupplierInfo supplierInfo){
        System.out.println("vendorSearchCriteria#$%^&*("+request.getParameter("vendorSearchCriteria"));
        int total=supplierInformationServiceInter.searchSupplierInfoData(null).size();

        List<SupplierInfo> supplierInfos = supplierInformationServiceInter.searchSupplierInfoData(supplierInfo);

        Map<String , Object> map = new HashMap<>();
        map.put("code" , 0);
        map.put("msg","");
        map.put("count" , total);
        map.put("data" , supplierInfos);

        String supplierInfoDataJosnStr= JSON.toJSONString(map);
        return supplierInfoDataJosnStr;
    }

    /**
     * 新增供应商信息
     * @param httpServletRequest
     * @param supplierInfo
     * @return
     */
    @RequestMapping(value = {"/insertSupplierInfoData"})
    @ResponseBody
    public String insertSupplierInfoData(HttpServletRequest httpServletRequest, SupplierInfo supplierInfo){
        supplierInfo.setSupId(new Tool().aotomaticId("supId" , toolInter));
        System.out.println("supplierInfo:"+supplierInfo);
        int rows = supplierInformationServiceInter.insertSupplierInfoData(supplierInfo);
        if(rows>0){
            System.out.println("新增成功");
        }else{
            System.out.println("新增失败");
        }
        return "";
    }

    /**
     * 修改供货商
     * @param supplierInfo
     * @return
     */
    @RequestMapping(value = {"/updateSupplierInfoData"})
    @ResponseBody
    public String updateSupplierInfoData(SupplierInfo supplierInfo){
        System.out.println();
        int rows = supplierInformationServiceInter.updateSupplierInfoData(supplierInfo);
        if(rows>0){
            System.out.println("修改成功");
        }else{
            System.out.println("修改失败");
        }
        return "";
    }

    /**
     * 删除供货商
     * @param supplierInfo
     * @return
     */
    @RequestMapping(value = {"/deleteSupplierInfoData"})
    @ResponseBody
    public String deleteSupplierInfoData(SupplierInfo supplierInfo){
        int rows = supplierInformationServiceInter.deleteSupplierInfoData(supplierInfo);
        if(rows>0){
            System.out.println("删除成功");
        }else{
            System.out.println("删除失败");
        }
        return "";
    }

}
